package com.achievement.controller;

import com.achievement.domain.dto.ProductTransformDTO;
import com.achievement.domain.param.ProductTransformParam;
import com.achievement.domain.vo.ProductTransformVO;
import com.achievement.service.IProductTransformService;
import com.alibaba.fastjson2.JSONObject;
import com.common.annotation.Anonymous;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/achievement/transform")
@Api(tags = "成果管理模块---科研成果转化管理")
public class ProductTransformController extends BaseController
{
    @Autowired
    private IProductTransformService productTransformService;


    @PreAuthorize("@ss.hasPermi('achievement:productTransform:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页获取科研成果转化列表", notes = "分页获取科研成果转化列表")
    public TableDataInfo list(ProductTransformParam param)
    {
        startPage();
        List<ProductTransformVO> list = productTransformService.selectByPage(param);
        return getDataTable(list);
    }

    /**
     * 科研成果转化成果导出
     */
    @PreAuthorize("@ss.hasPermi('achievement:productTransform:export')")
    @Log(title = "科研成果转化导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "科研成果转化导出", notes = "科研成果转化导出")
    public void export(HttpServletResponse response, @RequestBody Map<String,Object> requestMap)
    {
        ProductTransformParam param = JSONObject.parseObject(JSONObject.toJSONString(requestMap), ProductTransformParam.class);
        List<String> selectedColumns = (List<String>) requestMap.get("selectedColumns");

        List<ProductTransformVO> list = productTransformService.getListByConditions(param);
        ExcelUtil<ProductTransformVO> util = new ExcelUtil<>(ProductTransformVO.class);
        util.showColumn(selectedColumns.toArray(new String[0]));
        util.exportExcel(response, list, "科研成果转化导出数据");
    }

    /**
     * 获取科研成果转化详细信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:productTransform:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取科研成果转化详细信息", notes = "获取科研成果转化详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(productTransformService.getInfoById(id));
    }

    /**
     * 新增科研成果转化
     */
    @PreAuthorize("@ss.hasPermi('achievement:productTransform:add')")
    @Log(title = "新增科研成果转化", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增科研成果转化", notes = "新增科研成果转化")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ProductTransformDTO dto)
    {
        return productTransformService.add(dto);
    }

    /**
     * 修改科研成果转化
     */
    @PreAuthorize("@ss.hasPermi('achievement:productTransform:edit')")
    @Log(title = "修改科研成果转化", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改科研成果转化", notes = "修改科研成果转化")
    @PostMapping(value = "/update")
    public AjaxResult edit(@RequestBody ProductTransformDTO dto)
    {
        return productTransformService.update(dto);
    }

    /**
     * 删除科研成果转化
     */
    @PreAuthorize("@ss.hasPermi('achievement:productTransform:remove')")
    @Log(title = "删除科研成果转化", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除科研成果转化", notes = "删除科研成果转化")
	@DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(productTransformService.batchRemove(ids));
    }
}
